<!DOCTYPE HTML>
<html>
<head>
	<meta charset=utf-8>
	<title>剪刀石头布--饼子作品</title>
	<meta name=viewport content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no,target-densitydpi=device-dpi"/>
	<style>

	</style>
</head>

<body>
	<script src="js/phaser.min.js" type="text/javascript"></script>
	<script type="text/javascript">
        var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', {
            preload: preload,
            create: create,
            update: update,
            render: render
        });
        var platforms;
        var player;
        var cursors;
        var stars;
        var score;
        var scoreCount = 0;
        var ledge2;

        function preload() {

            game.load.image('sky', 'img/logo.png');
            game.load.image('ground', 'http://img.blog.csdn.net/20140315155808296?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjg0NDcxOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center');
            game.load.image('star', 'http://img.blog.csdn.net/20140315155732312?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjg0NDcxOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center');
            game.load.spritesheet('dude', 'http://img.blog.csdn.net/20140315155820343?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjg0NDcxOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center', 32, 48);

        }

        function create() {
            game.physics.setBoundsToWorld();
            game.add.sprite(0, 0, 'sky');

            //  The platforms group contains the ground and the 2 ledges we can jump on
            platforms = game.add.group();
            platforms.enableBody = true;
            platforms.physicsBodyType = Phaser.Physics.ARCADE;
            // Here we create the ground.
            var ground = platforms.create(0, game.world.height - 64, 'ground');

            //  Scale it to fit the width of the game (the original sprite is 400x32 in size)
            ground.scale.setTo(2, 2);

            //  This stops it from falling away when you jump on it
            ground.body.immovable = true;

            //  Now let's create two ledges
            var ledge = platforms.create(400, 400, 'ground');
            ledge.body.immovable = true;

            ledge = platforms.create(-150, 250, 'ground');

            ledge.body.immovable = true;



            //ledge2.body.setPolygon(62,112, 100,120,88,40);
            player = game.add.sprite(32, game.world.height - 450, 'dude');
            game.physics.enable(player, Phaser.Physics.ARCADE);
            console.log(player.body);

            player.body.bounce.y = 0.6;
            player.body.gravity.y = 400;
            player.body.collideWorldBounds = true;
            player.animations.add('left', [0, 1, 2, 3], 10, true);
            player.animations.add('right', [5, 6, 7, 8], 10, true);


            stars = game.add.group();
            stars.enableBody = true;
            stars.physicsBodyType = Phaser.Physics.ARCADE;
            for (var i = 0; i < 12; i++) {
                var star = stars.create(i * 70, 0, "star");
                star.body.gravity.y = 400;
                star.body.bounce.y = 0.7 + Math.random() * 0.2;
                star.body.collideWorldBounds = true;
            }
            cursors = game.input.keyboard.createCursorKeys();

            score = game.add.text(12, 12, "score:" + scoreCount + "", {
                color: "#000"
            });



            //var tilemap = game.add.tilemap("star",44,44,44,44);

        }

        function render() {
            //game.debug.renderPhysicsBody(ledge2.body);
        }

        function update() {
            game.physics.arcade.collide(player, platforms);
            game.physics.arcade.collide(stars, platforms);
            game.physics.arcade.collide(player, ledge2);
            game.physics.arcade.overlap(player, stars, function (player, star) {
                console.log(stars.total);
                star.kill();
                scoreCount++;
                score.text = "score:" + scoreCount + "";
                if (stars.total == 0) {
                    alert("congratulation!");
                }
            }, null, this);
            //  Reset the players velocity (movement)
            player.body.velocity.x = 0;

            if (cursors.left.isDown) {
                //  Move to the left
                player.body.velocity.x = -150;

                player.animations.play('left');
            } else if (cursors.right.isDown) {
                //  Move to the right
                player.body.velocity.x = 150;

                player.animations.play('right');
            } else {
                //  Stand still
                player.animations.stop();

                player.frame = 4;
            }

            //  Allow the player to jump if they are touching the ground.
            if (cursors.up.isDown && player.body.touching.down) {
                player.body.velocity.y = -350;
            }
        }
	</script>
</body>
</html>